package com.qding.park.manager.service.statis.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.facade.statis.service.InviteStatisFacade;
import com.qding.park.manager.pojo.TbInvitecard;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.enums.VisitPurposeEnum;
import com.qding.park.manager.pojo.example.TbInviteExample;
import com.qding.park.manager.pojo.example.TbInviteExample.Criteria;
import com.qding.park.manager.service.statis.biz.InviteStatisBiz;
import com.qding.park.manager.service.statis.mapper.TbInvitecardStatisMapper;
import com.qding.park.manager.service.statis.mapper.TbParkspaceStatisMapper;

/**
 * @author Administrator 邀请单统计服务
 */
@Service("inviteStatisFacade")
public class InviteStatisFacadeImpl implements InviteStatisFacade {
    private static final Logger _log = LoggerFactory.getLogger(InviteStatisFacadeImpl.class); 

	@Autowired
	private TbInvitecardStatisMapper tbInvitecardMapper;

	@Autowired
	private InviteStatisBiz inviteStatisBiz;
	
	@Autowired
    private TbParkspaceStatisMapper tbParkspaceMapper;

	@Override
	public ParkDataResult invitelist(TbInviteExample example, int jumpPage, int pageLength) {

		Criteria criteria = example.getOredCriteria().get(0);

		// 以订单创建时间简
		example.orderByCreateTimeDesc();
		PageHelper.startPage(jumpPage, pageLength);

		List<TbInvitecard> invitecardList = tbInvitecardMapper.listByExample(example);
		
		PageInfo page = new PageInfo(invitecardList);
		List dataList = new ArrayList();

		int totalFee = 0;
		for (TbInvitecard tbInvitecard : invitecardList) {

//			Map<String, Object> map = new HashMap<String, Object>();
			// // 订单号
			// map.put("orderNum", tbInvitecard.getId());
			// // 订单状态
			// map.put("orderStatus",
			// inviteStatisBiz.getOrderStatusStr(tbInvitecard));
			// // 手机号
			// map.put("tel", tbInvitecard.getTel());
			// // 车牌
			// map.put("carNum", tbInvitecard.getCarNum());
			// // 车场
			// map.put("visitTime", tbInvitecard.getStartTime() == null ? ""
			// : DateUtils.formatDate(tbInvitecard.getStartTime(),
			// DateUtils.DATE_FORMAT_DATETIME));
			//
			// map.put("visitPurpose",
			// VisitPurposeEnum.tranDesc(tbInvitecard.getVisitPurpose()));
			//// // 预定结束时间
			//// map.put("endTime", tbInvitecard.getEndTime() == null ? ""
			//// : DateUtils.formatDate(tbInvitecard.getEndTime(),
			// DateUtils.DATE_FORMAT_DATETIME));
			//
			// // 当前状态
			// map.put("currentStatus",
			// inviteStatisBiz.getCurrentStatus(tbInvitecard));
			dataList.add(bean2map(tbInvitecard));
		}

		ParkDataResult parkDataResult = ParkDataResult.createListDate(dataList, page);
		return parkDataResult;
	}

	private Map<String, Object> bean2map(TbInvitecard tbInvitecard) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(tbInvitecard==null){
			return map;
		}
		// 订单号
		map.put("orderNum", tbInvitecard.getId());
		// 订单状态
		map.put("orderStatus", inviteStatisBiz.getOrderStatusStr(tbInvitecard));
		// 手机号
		map.put("tel", StringUtils.isBlank( tbInvitecard.getTel())?"":tbInvitecard.getTel());
		// 车牌
		map.put("carNum", tbInvitecard.getCarNum());
		// 车场
		map.put("inviteTime", tbInvitecard.getStartTime() == null ? 0
				: tbInvitecard.getStartTime().getTime()/1000);

		map.put("visitPurpose", VisitPurposeEnum.tranDesc(tbInvitecard.getVisitPurpose()));
		// // 预定结束时间
		// map.put("endTime", tbInvitecard.getEndTime() == null ? ""
		// : DateUtils.formatDate(tbInvitecard.getEndTime(),
		// DateUtils.DATE_FORMAT_DATETIME));

		// 当前状态
		map.put("currentStatus", inviteStatisBiz.getCurrentStatus(tbInvitecard));
		
		return map;

	}

	@Override
	public ParkDataResult orderInfoById(String orderId) throws BaseException {
		TbInvitecard  tbInvitecard=tbInvitecardMapper.statisById(orderId);
		ParkDataResult parkDataResult = ParkDataResult.createObjDate();
		parkDataResult.putMapOnObj(bean2map(tbInvitecard));
		return parkDataResult;
	}

    @Override
    public ParkDataResult inviteParams(int parkId) throws BaseException {
        TbParkspace park=tbParkspaceMapper.queryParkInfoByParkId(parkId);
        
        if(park==null){
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getDesc(), "没有找到对应的车场信息",
                            "parkId=" + parkId));
        }
        
        ParkDataResult dataResult=ParkDataResult.createObjDate();
        dataResult.putDataOnObj("visitorCheck", park.getVisitingCheck());
        return dataResult;
    }

    @Override
    public ParkDataResult setInviteParams(int parkId, int visitorCheck) throws BaseException {
        int num=tbParkspaceMapper.visitorCheckSet(visitorCheck,parkId);
        return ParkDataResult.createEmpty();
    }


}
